package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;

import com.example.yuejian.yuejian.R;

public class WelcomeActivity extends AppCompatActivity {
    //是否是第一次进入程序
    private static boolean isFirstIn=false;
    private static final int TIME=2000;
    private static final int GO_HOME=1000;
    private static final int GO_GUIDE=1001;

    private Handler mHandler=new Handler(){

        /**
         * 使用handleMessage方法来决定跳转到哪个界面
         * @param msg
         */
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case GO_GUIDE:
                    goGuide();
                    break;
                case GO_HOME:
                    goHome();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        init();
    }
    /**
     * 进行是否是第一次进入应用的判断
     */
    private void init(){
        //新建SharedPreferences对象
        SharedPreferences sharedPreferences=getSharedPreferences("test",MODE_PRIVATE);
        //查询值，不存在赋值为true;
        isFirstIn=sharedPreferences.getBoolean("isFirstIn",true);
        //已经存在该对象，直接进入主界面
        if(!isFirstIn){
            mHandler.sendEmptyMessageDelayed(GO_HOME,TIME);//延时发送
        }else {
            //不存在，进入引导界面
            mHandler.sendEmptyMessageDelayed(GO_GUIDE, TIME);
            //存储内容
            SharedPreferences.Editor editor=sharedPreferences.edit();
            editor.putBoolean("isFirstIn",false);
            editor.commit();//提交
        }

    }
    /**
     *进入主界面
     */
    private void goHome(){
        startActivity(new Intent(WelcomeActivity.this,MainActivity.class));
        finish();
    }


    /**
     *进入引导界面
     */
    private void goGuide(){
        startActivity(new Intent(WelcomeActivity.this,GuideActivity.class));
        finish();
    }
}


